<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="66"
      required
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button class='post-btn'
     @click="onPost"
     :disabled='!message.length'
     >发布</van-button>
  </div>
</template>

<script>
import {addComment}from'@/api/comment'
export default {
  name: 'CommentPost',
  components: {},
  props: {
      target:{
           type: [Number, String, Object],
      require: true

      },
      
     type:{
      type:String,
// 验证
       validator (value) {
        return ['a', 'c'].includes(value)
      },  
  },},
inject: {
    articleId: {
        type: [Number, String, Object],
        default: null
    }
},

  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {

      async onPost(){
        
       this.$toast.loading({
  message: '加载中...',
  forbidClick: true,
});

          try {
              const {data} = await addComment({
                //   评论的目标id（评论文章即为文章id)
                  target:this.target.toString(),
                 content: this.message,//评论内容
                 art_id: this.type==='a'? null:this.articleId.toString()
              })
            
            //   弹出层关闭
              this.$emit('show',data.data)
              this.message=''

              this.$toast('发布完成');
          } catch (error) {
              console.log(error);
              Toast.fail('失败文案');
          }
      }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    background: #fff;
    &::before {
      display: none;
    }
  }
}
</style>